package kr.hkit.summary_java.serviceUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import javax.swing.table.TableModel;

import kr.hkit.myframework.db.ConnectionFactory;
import kr.hkit.summary_java.dao.DepartmentDAO;
import kr.hkit.summary_java.erp.dto.Department;

public class MainFrame extends JFrame implements ActionListener {

	private JPanel contentPane;
	private Connection con;
	private DepartmentDAO dao;
	private JMenuItem d_mntmAdd;//추가
	private JMenuItem d_mntmUpdate;//수정
	private JMenuItem d_mntmDelete;//삭제
	private JMenuItem d_mntmSelect;//검색
	private JMenuItem d_menuList;
	private JTable table;
	private JMenuItem e_mntmAdd;
	private JMenuItem e_mntmUpdate;
	private JMenuItem e_mntmDelete;
	private JMenuItem e_mntmSelect;
	private JMenuItem e_mntmList;
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//Connection con= ConnectionFactory.getConnection();
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	public MainFrame() {
		initialize();
	}
	private void initialize() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 367, 310);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		//contentPane.add(menuBar, BorderLayout.NORTH);
		
		JMenu department = new JMenu("부서");
		menuBar.add(department);
		
		d_mntmAdd = new JMenuItem("추가");
		d_mntmAdd.addActionListener(this);
		department.add(d_mntmAdd);
		
		d_mntmUpdate = new JMenuItem("수정");
		d_mntmUpdate.addActionListener(this);
		department.add(d_mntmUpdate);
		
		d_mntmDelete = new JMenuItem("삭제");
		d_mntmDelete.addActionListener(this);
		department.add(d_mntmDelete);
		
		d_mntmSelect = new JMenuItem("검색");
		d_mntmSelect.addActionListener(this);
		department.add(d_mntmSelect);
		
		d_menuList = new JMenuItem("리스트");
		d_menuList.addActionListener(this);
		department.add(d_menuList);
		
		JMenu employee = new JMenu("사원");
		menuBar.add(employee);
		
		e_mntmAdd = new JMenuItem("추가");
		e_mntmAdd.addActionListener(this);
		employee.add(e_mntmAdd);
		
		e_mntmUpdate = new JMenuItem("수정");
		e_mntmUpdate.addActionListener(this);
		employee.add(e_mntmUpdate);
		
		e_mntmDelete = new JMenuItem("삭제");
		e_mntmDelete.addActionListener(this);
		employee.add(e_mntmDelete);
		
		e_mntmSelect = new JMenuItem("검색");
		e_mntmSelect.addActionListener(this);
		employee.add(e_mntmSelect);
		
		e_mntmList = new JMenuItem("리스트");
		employee.add(e_mntmList);
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		dao = new DepartmentDAO(con);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == e_mntmSelect) {
			actionPerformedE_mntmSelect(e);
		}
		if (e.getSource() == e_mntmDelete) {
			actionPerformedE_mntmDelete(e);
		}
		if (e.getSource() == e_mntmUpdate) {
			actionPerformedE_mntmUpdate(e);
		}
		if (e.getSource() == e_mntmAdd) {
			actionPerformedE_mntmAdd(e);
		}
		if (e.getSource() == d_menuList) {
			actionPerformedMenuList(e);
		}
		if (e.getSource() == d_mntmSelect) {
			actionPerformedMntmNewMenuItem_3(e);
		}
		if (e.getSource() == d_mntmDelete) {
			actionPerformedMntmNewMenuItem_2(e);
		}
		if (e.getSource() == d_mntmUpdate) {
			actionPerformedMntmNewMenuItem_1(e);
		}
		if (e.getSource() == d_mntmAdd) {
			actionPerformedMntmNewMenuItem(e);
		}
	}
	//department(버튼클릭시추가화면)
	protected void actionPerformedMntmNewMenuItem(ActionEvent e) {//메뉴를 클릭하였을때 추가가 보이는 액션 
		
		PanelDepartmentUI input = new PanelDepartmentUI();
		contentPane.removeAll();//패널을 지우고 
		contentPane.add(input);
		input.setBtnAdd("추가");//버튼을 공동으로 사용하기 때문에 메인에서 액션사용시 바꿔준다.
		revalidate();//재확
	
		
	}
	//department(버튼클릭시수정화면)
	protected void actionPerformedMntmNewMenuItem_1(ActionEvent e) {//메뉴를 클릭하였을때 수정이 보이는 액션
		PanelDepartmentUI update= new PanelDepartmentUI();
		contentPane.removeAll();
		contentPane.add(update);
		update.setBtnAdd("수정");
		revalidate();
	
	}
	//department(버튼클릭시삭제화면)
	protected void actionPerformedMntmNewMenuItem_2(ActionEvent e) {
		PanelDepartmentUI delete= new PanelDepartmentUI();
		contentPane.removeAll();
		contentPane.add(delete);
		delete.setBtnAdd("삭제");
		revalidate();
	
	}
	//department(버튼클릭시검색화면)
	protected void actionPerformedMntmNewMenuItem_3(ActionEvent e) {
		Connection con= ConnectionFactory.getConnection();
		dao = new DepartmentDAO(con);
		PanelDepartmentUI search= new PanelDepartmentUI();
		contentPane.removeAll();
		contentPane.add(search);
		search.setBtnAdd("검색");
		revalidate();
	}
	//리스트는 클릭시 바로 보여져야 하므로 메인에서 액션
	protected void actionPerformedMenuList(ActionEvent e) {
		//DB에서 리스트의 값들을 연결시켜야 함
		Connection con= ConnectionFactory.getConnection();
		dao = new DepartmentDAO(con);
		ArrayList<Department> list= new ArrayList<Department>();
		String [][] arrayList = null;
		try{
			
			list= dao.listItems();
			System.out.println(list);
			arrayList= new String[list.size()][3];
			for(int i=0;i<list.size();i++){//Department에서 각 리스트를 가져오기
				Department d = list.get(i);
				arrayList[i]=new String[]{d.getDeptNo()+"", d.getDeptName(), d.getFloor()+""};
			}
		}catch(SQLException e1){
				e1.printStackTrace();
			}finally{
				String[] col={"부서번호","부서명","위치"};
				TableModel model= new DefaultTableModel(arrayList, col);
				table = new JTable();
				table.setModel(model);
				
				JScrollPane scrollPane = new JScrollPane(table);
				getContentPane().add(scrollPane);
				cellAndwidth();
				table.setAutoCreateRowSorter(true);
				setVisible(true);
				scrollPane.setViewportView(table);
			}
		}
		private void cellAndwidth(){//리스트에서 위치 정렬
			DefaultTableCellRenderer renderer= new DefaultTableCellRenderer();
			renderer.setHorizontalAlignment(SwingConstants.CENTER);
			
			TableColumnModel tcModel=table.getColumnModel();
			tcModel.getColumn(0).setPreferredWidth(70);
			tcModel.getColumn(0).setCellRenderer(renderer);
			
			tcModel.getColumn(1).setPreferredWidth(70);
			tcModel.getColumn(1).setCellRenderer(renderer);
			
			tcModel.getColumn(2).setPreferredWidth(70);
			tcModel.getColumn(2).setCellRenderer(renderer);
		}
	//employee(버튼클릭시검색화면)
	protected void actionPerformedE_mntmAdd(ActionEvent e) {
		PanelEmployeeUI input= new PanelEmployeeUI();
		contentPane.removeAll();
		contentPane.add(input);
		input.setBtnNewButton("추가");
		revalidate();
		
	}
	//employee(버튼클릭시수정화면)
	protected void actionPerformedE_mntmUpdate(ActionEvent e) {
		PanelEmployeeUI update= new PanelEmployeeUI();
		contentPane.removeAll();
		contentPane.add(update);
		update.setBtnNewButton("수정");
		revalidate();
	}
	protected void actionPerformedE_mntmDelete(ActionEvent e) {
		PanelEmployeeUI delete= new PanelEmployeeUI();
		contentPane.removeAll();
		contentPane.add(delete);
		delete.setBtnNewButton("삭제");
		revalidate();
	}
	protected void actionPerformedE_mntmSelect(ActionEvent e) {
		PanelEmployeeUI select= new PanelEmployeeUI();
		contentPane.removeAll();
		contentPane.add(select);
		select.setBtnNewButton("검색");
		revalidate();
	}
	}

